var hourInput = document.getElementsByClassName('hours')[0],
    minuteInput = document.getElementsByClassName('minutes')[0],
    secondInput = document.getElementsByClassName('seconds')[0],
    start = document.getElementsByTagName('button')[0],
    reset = document.getElementsByTagName('button')[1],
    pause = document.getElementsByTagName('button')[2],
    startKey = true,
    pauseKey = false,
    resetKey = false,
    timer;

start.addEventListener('click', function() {
    if(startKey) {
        startTimer();
    }
}, false);

reset.addEventListener('click', function() {
    if (resetKey) {
        window.location.reload();
    }
}, false);

pause.addEventListener('click', function() {
    if(pauseKey) {
        pauseTimer();
    }
}, false);

function startTimer() {
    timer = setInterval(judge, 1000);
    
    startKey = false;
    pauseKey = true;
    resetKey = true;
    start.className = 'defaultC';
    pause.className = 'pauseC';
    secondInput.setAttribute('disabled', 'disabled');
    minuteInput.setAttribute('disabled', 'disabled');
    hourInput.setAttribute('disabled', 'disabled');
    secondInput.style.color = '#fff';
    minuteInput.style.color = '#fff';
    hourInput.style.color = '#fff';
    // 注意disabled 和 readonly 的区别
}

function pauseTimer() {
    clearInterval(timer);
    pause.className = 'defaultC';
    start.className = 'startC';
    pauseKey = false;
    startKey = true;
}

function judge() {
    //1.输入的值正常时
    if(secondInput.value >= 0 && secondInput.value < 60 && minuteInput.value < 60 && minuteInput.value >= 0 && hourInput.value >= 0) {
        if(secondInput.value != 0) {
            secondInput.value --;
        } else {
            if(minuteInput.value != 0) {
                minuteInput.value --;
                secondInput.value = 59;
            } else {
                if(hourInput.value != 0) {
                    hourInput.value --;
                    minuteInput.value = 59;
                    secondInput.value = 59;
                } else {
                    clearInterval(timer);
                    window.alert('Time out!!!');
                    window.location.reload();
                }
            }
        }
    } else if (secondInput.value < 0 || minuteInput.value < 0 || hourInput.value < 0) {
        // 2.有输入框中输入的值小于0
        if (secondInput.value < 0) {
            secondInput.value = ' 00 ';
        }
        if (minuteInput.value < 0) {
            minuteInput.value = ' 00 ';
        }
        if (hourInput.value < 0) {
            hourInput.value = ' 00 ';
        }
        judge();
    } else {
        // 3.当存在输入框的值大于等于60
        if (secondInput.value >= 60) {
            minuteInput.value ++;
            secondInput.value -= 60;
        }
        if (minuteInput.value >= 60) {
            hourInput.value ++;
            minuteInput.value -= 60;
        }
    judge();
    }
}
/*
readonly规定输入字段为只读可复制，
但是，可选择,可以接收焦点，还可以选中或拷贝其文本;
<input type="text" readonly="readonly">
disabled ,设置后文字的颜色会变成灰色。
无法与 <input type="hidden"> 一起使用;
<input type="text" disabled="disabled"></input>
*/
